package com.xbs.controller.user;

import java.io.IOException;
import java.sql.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xbs.controller.base.BaseServlet;
import com.xbs.entity.MyCenterEntity;
import com.xbs.service.user.MyCenterService;
import com.xbs.utils.DateJsonValueProcessor;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

@WebServlet("/userview/myCenter/duan1")
public class MyCenter_duan1 extends BaseServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String uname = (String) req.getSession().getAttribute("uname");
		String status1;
		Date date1;
		Date date2;
		if(req.getParameter("fromDate")==null || req.getParameter("fromDate") == ""){
			date1=null;
		}else{
			date1= Date.valueOf(req.getParameter("fromDate"));
		}
		if(req.getParameter("toDate")==null ||req.getParameter("toDate") == ""){
			date2=null;
		}else{
			date2= Date.valueOf(req.getParameter("toDate"));
		}
		if(req.getParameter("type_name") == null ||  req.getParameter("type_name") == "" || req.getParameter("type_name").equals("全部")){
			status1 = "";
		}else{
			status1=req.getParameter("type_name");
		}
		MyCenterService mycenterservice = new MyCenterService();
		List<MyCenterEntity> mycenterlist = mycenterservice.getDuan(uname, status1, date1, date2);
		JsonConfig jsonConfig = new JsonConfig();   
		jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);   
		jsonConfig.registerJsonBeanProcessor(java.sql.Date.class, new DateJsonValueProcessor());  
		jsonConfig.setExcludes(new String[]{"handler","hibernateLazyInitializer"});    
		JSONArray jsonArray = JSONArray.fromObject(mycenterlist, jsonConfig);
		resp.getWriter().print(jsonArray.toString());
	}
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		double money = Double.valueOf(req.getParameter("money1")).doubleValue();
		String coid = req.getParameter("order");
		MyCenterService mycenterservice = new MyCenterService();
		mycenterservice.update(money, "预定成功", coid);
		req.getRequestDispatcher("/userview/myCenter/duan").forward(req, resp);
	}
}
